<?php
ini_set('max_execution_time', '0');
include './vendor/autoload.php';// 使用composer安装后引入目录
include_once 'class/Mysql.php';
$config=[
    'ak' => 'fSvRlbizGwxjKVM0CewkZuJIxQkcZfZ0ushbTEp3',
    'sk' => 'E0POvtUZOoRi4qqXXPbhET5wmSD4UUy8XACg7J-f',
    'bucket' => 'ccuke',
    'image_url' => 'q8qcqgycl.bkt.clouddn.com',
];


// 构建鉴权对象
$auth = new \Qiniu\Auth($config['ak'],$config['sk']);
// 初始化UploadManager类
$uploadMgr = new \Qiniu\Storage\UploadManager();

$db = file_get_contents('db/upload.db');
$db = $db+1;

//读取文件数据
$data =file_get_contents('data/data.csv');
$data = explode("\n",$data);
$data = array_filter($data);
foreach ($data as $k=>$v){
    if($k < $db) continue;
    $decodeData = json_decode($v,true);
    $filename=$decodeData['localFileName'];
    if(empty($filename)){
        file_put_contents('logs/fileName_error.log',$v.PHP_EOL,FILE_APPEND);
        file_put_contents('db/upload.db',$db);
        continue;
    }
    $file='images/'.$filename;
    // 生成上传需要的token
    $token = $auth->uploadToken($config['bucket']);
    list($res,$err) = $uploadMgr->putFile($token,$filename,$file);
    if($err !== null){
        $err[]=print_r($err,1);
        $err[]=$v;
        file_put_contents('logs/upload_error.log',json_encode($err,258).PHP_EOL,FILE_APPEND);
    }else{
        try {
            //更新数据库
            $mysql = Mysql::getMysql();
            $obj = $mysql->query("update source set status =2 where id= " . $decodeData['id']);
            $result = $obj->execute();
            $imageURL = 'http://q8qcqgycl.bkt.clouddn.com/' . $filename . '?imageMogr2/thumbnail/!50p';
            $pickURL = 'https://www.stickpng.com/assets/images/' . $filename;
            $sql="update pic_info set image ='" . $imageURL . "' where pick_up_url= '" . $pickURL . "'";
            file_put_contents('logs/sql.log',json_encode($sql,258).PHP_EOL,FILE_APPEND);
            $obj = $mysql->query($sql);
            $result = $obj->execute();
        }catch (Exception $e){
            file_put_contents('logs/sql_error.log',json_encode($sql,258).PHP_EOL,FILE_APPEND);
        }
    }
    file_put_contents('db/upload.db',$db);
}






?>